<?php

namespace app\admin\model;

use app\common\model\TimeModel;

class SystemBranch extends TimeModel
{

    protected $name = "system_branch";

    protected $pk = 'branch_id';

    protected $deleteTime = false;

    public function admins()
    {
        return $this->hasMany('app\admin\model\SystemAdmin', 'from_branch', 'branch_id');
    }

    /**
     * @title 按分组获取管理员
     * @param int $branch_id
     * @param string|array $field
     * @return array
     * @author FashionJune
     */
    public function getGroupList($branch_id = 0, $field = '*')
    {
        if ($branch_id > 0) {//按分组获取
            $where['branch_id'] = $branch_id;
        } else {
            $where = true;
        }
        return $this->where($where)
            ->with([
                'admins' => fn($query) => $query->field(['id', 'from_branch' ,'username', 'remark'])
            ])->field($field)->order('sort', 'desc')->select()->toArray();
    }


}